package com.cloud.tsp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.api.domain.model.*;
import com.cloud.api.utils.ExcelUtils;
import com.cloud.tsp.mapper.EvModelInformationMapper;
import com.cloud.tsp.service.IEvModelInformationService;
import com.cloud.tsp.service.IOilModelInformationService;
import com.cloud.tsp.service.IVehicleModelService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import java.lang.reflect.Field;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author haohaibo
 * @since 2023-12-20
 */
@Service("evModelInformationService")
public class EvModelInformationServiceImpl extends ServiceImpl<EvModelInformationMapper, EvModelInformation> implements IEvModelInformationService {

    @Resource
    private EvModelInformationMapper evModelInformationMapper;
    @Resource
    private IVehicleModelService vehicleModelService;
    @Resource
    private IOilModelInformationService oilModelInformationService;
    @Resource
    private MessageSource messageSource;

    @Override
    public String selectNoticeModelById(String id) {
        String noticeModel = null;
        VehicleModel model = vehicleModelService.getById(id);
        if (model == null){
            return null;
        }
        String type = model.getType();
        if ("ev".equals(type)){
            EvModelInformation evModelInformation = evModelInformationMapper.selectById(id);
            if (evModelInformation != null){
                noticeModel = evModelInformation.getNoticeModel();
            }
        }
        else {
            OilModelInformation oilModelInformation = oilModelInformationService.getById(id);
            if (oilModelInformation != null){
                noticeModel = oilModelInformation.getNoticeModel();
            }
        }
        return noticeModel;
    }

    @Override
    public EvModelInformation selectByModelId(String id) {
        return evModelInformationMapper.selectById(id);
    }

    @Override
    public <T> void InsertOrUpdate(T para) {
        //检查para中除了id之外有没有数据
        if (!checkFieldsExceptId(para)){
            return;
        }
        EvModelInformation evModelInformation = new EvModelInformation();
        BeanUtils.copyProperties(para, evModelInformation);

        this.saveOrUpdate(evModelInformation);
    }

    public static boolean checkFieldsExceptId(Object obj){
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            if (!field.getName().equals("id")) {
                Object value = null;
                try {
                    value = field.get(obj);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                if (value != null) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void exportEvInformation(String id, HttpServletResponse response) throws IOException {
        Locale locale = LocaleContextHolder.getLocale();
        String fileName = "table.modelInformation";
        EvInformationExcel evInformationExcel = new EvInformationExcel();
        EvModelInformation evModelInformation = evModelInformationMapper.selectById(id);
        ModelName modelName = vehicleModelService.selectParentNameById(id);
        BeanUtils.copyProperties(evModelInformation,evInformationExcel);
        BeanUtils.copyProperties(modelName,evInformationExcel);
        fileName = messageSource.getMessage(fileName, null, fileName, locale);
        List<EvInformationExcel> list = new ArrayList<>();
        list.add(evInformationExcel);
        ExcelUtils.export(response,fileName,list,EvInformationExcel.class);
    }

    @Override
    public List<NoticeModel> selectNoticeModelByModel() {
        return evModelInformationMapper.selectNoticeModelByModel();
    }
}